/*
 * Copyright 2004-2008 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.yakaten.action;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.arnx.jsonic.JSON;
import net.arnx.jsonic.JSONException;

import org.seasar.framework.log.Logger;
import org.seasar.struts.annotation.Execute;

import com.yakaten.dto.YahooItemInfoDto;
import com.yakaten.dto.YahooPriceTaskDto;
import com.yakaten.exception.MakeShopException;
import com.yakaten.exception.SystemException;
import com.yakaten.exception.YahooException;
import com.yakaten.logic.YahooPriceLogic;
import com.yakaten.service.MakeShopItemService;
import com.yakaten.service.YahooItemService;
import com.yakaten.service.YahooPriceRunner;


public class YahooPriceAction extends BaseAction {
	static Logger logger = Logger.getLogger(YahooPriceAction.class);

	public String yahooItemId;
	public String yahooItemName;
	public String yahooItemPrice;
	public String yahooItemCategory;
	public String shopItemId;
	public String shopItemName;
	public String shopItemPrice;
	public String minPrice;
	public String comparePrice;
	public String comparePricePer;

	public String taskNo;
	public int status;
	public String quantity;
	public int start;
	public int limit;
	public String searchKey;

	public String kankakuTime;

	@Execute(validator = false)
	public String index() {
		return "../yahooprice.jsp";
	}


	@Execute(validator = false)
	public String doSetKankakuTime() throws IOException {
		try {
			YahooPriceRunner.KENKAKU = Integer.parseInt(kankakuTime);
			response.getWriter().write("success");
		} catch (Exception e) {
			response.getWriter().write("error");
		}
		return null;
	}

	@Execute(validator = false)
	public String doGetKankakuTime() throws IOException {
		response.getWriter().write("" + YahooPriceRunner.KENKAKU);
		return null;
	}

	@Execute(validator = false)
	public String doGetYahooItemInfo() throws IOException {
		YahooItemService yahooItemService = new YahooItemService();
		try {
			YahooItemInfoDto dto = yahooItemService.getItemInfo(yahooItemId);
			response.getWriter().write(
					dto.getItemName() + "," + dto.getItemPrice()+ "," + dto.getItemGenre());
		} catch (YahooException e) {
			logger.error(e);
			response.getWriter().write("取得失敗。");
		}
		return null;
	}

	@Execute(validator = false)
	public String doGetMyShopItemInfo() throws IOException {
		try {
			MakeShopItemService service = new MakeShopItemService();
			Map<String, String> valueMap = service.getItemInfoByHtml(shopItemId);

			String itemName = valueMap.get("itemName");
			String itemPrice =  valueMap.get("itemPrice");
			response.getWriter().write(itemName + "," + itemPrice+ "," + "1");

		} catch (MakeShopException e) {
			logger.error(e);
			response.getWriter().write("取得失敗。");
		}
		return null;
	}

	@Execute(validator = false)
	public String doGetAllTask() throws JSONException, IOException {
		List<YahooPriceTaskDto> taskList = new ArrayList<YahooPriceTaskDto>();
		int end = start+limit;
		try {
			taskList = new YahooPriceLogic().getAllTaskList();
			if(end>taskList.size()){
				end=taskList.size();
			}
		} catch (SystemException e) {
			logger.error(e);
		}
//		response.getWriter().write(
//				"{\"totalCount\":\"" + taskList.size() + "\",\"results\":"
//						+ JSON.encode(taskList.subList(start, end)) + "}");
		response.getWriter().write(
				"{\"totalCount\":\"" + taskList.size() + "\",\"results\":"
						+ JSON.encode(taskList) + "}");
		return null;
	}

	@Execute(validator = false)
	public String addTask() throws IOException {
		YahooPriceLogic logic = new YahooPriceLogic();
		YahooPriceTaskDto dto = new YahooPriceTaskDto();

		try {

			if(logic.isExistTask(yahooItemId,shopItemId)){
				response.getWriter().write("exist");
				return null;
			}else{
				dto.setTaskNo(logic.getNewTaskNo());
				dto.setYahooItemId(yahooItemId);
				dto.setYahooItemName(yahooItemName);
				dto.setYahooItemPrice(Integer.parseInt(yahooItemPrice));
				dto.setYahooItemCategory(yahooItemCategory);
				dto.setShopItemId(shopItemId);
				dto.setShopItemName(shopItemName);
				dto.setShopItemPrice(Integer.parseInt(shopItemPrice));
				dto.setMinPrice(Integer.parseInt(minPrice));
				dto.setStartPrice(dto.getYahooItemPrice());
				dto.setStartTime(new Date());
				dto.setStatus(1);
				dto.setQuantity(quantity);
				dto.setComparePrice(Integer.parseInt(comparePrice));
				dto.setComparePricePer(Integer.parseInt(comparePricePer));
				logic.addTask(dto);
			}

		} catch (SystemException e) {
			logger.error(e);
		}

		response.getWriter().write("success");
		return null;
	}

	@Execute(validator = false)
	public String doDeleteTask() throws IOException {
		YahooPriceLogic logic = new YahooPriceLogic();

		try {
			if(taskNo!=null && taskNo.length()>0){
				String []taskNos = taskNo.split(",");
				for (int i=0;i<taskNos.length;i++){
					logic.deleteTask(taskNos[i]);
				}
			}
		} catch (SystemException e) {
			logger.error(e);
		}


		response.getWriter().write("success");
		return null;
	}

	@Execute(validator = false)
	public String doUpdateTaskStatus() throws IOException {
		YahooPriceLogic logic = new YahooPriceLogic();
		try {
			logic.updateTaskStatus(taskNo, status);
		} catch (SystemException e) {
			logger.error(e);
		}
		response.getWriter().write("success");
		return null;
	}

	@Execute(validator = false)
	public String doUpdateTask() throws IOException {
		YahooPriceLogic logic = new YahooPriceLogic();
		YahooPriceTaskDto dto = new YahooPriceTaskDto();

		try {
			dto.setTaskNo(taskNo);
			dto.setYahooItemId(yahooItemId);
			dto.setYahooItemName(yahooItemName);
			dto.setYahooItemPrice(Integer.parseInt(yahooItemPrice));
			dto.setYahooItemCategory(yahooItemCategory);
			dto.setShopItemId(shopItemId);
			dto.setShopItemName(shopItemName);
			dto.setShopItemPrice(Integer.parseInt(shopItemPrice));
			dto.setMinPrice(Integer.parseInt(minPrice));
			dto.setStartPrice(dto.getYahooItemPrice());
			dto.setStartTime(new Date());
			dto.setStatus(status);
			dto.setQuantity(quantity);
			dto.setComparePrice(Integer.parseInt(comparePrice));
			dto.setComparePricePer(Integer.parseInt(comparePricePer));
			logic.updateTask(dto);

		} catch (SystemException e) {
			logger.error(e);
		}

		response.getWriter().write("success");
		return null;
	}

}
